/**
*****************************************************************************
 * @file switch.c
 * @brief SWITCH驱动
 * @author Moon
 * @version V1.0.0
 * @date 2021/12/23
****************************************************************************
 */
#include "dev_switch.h"
#include "stdbool.h"
#include "stm32f4xx.h"
#include "drv_f4_gpio.h"

/* Private Struct ---------------------------------------------------------------*/

/* Private defines ---------------------------------------------------------------*/

/* Private enum ---------------------------------------------------------------*/
/* Private Variable ---------------------------------------------------------------*/
//Pointer
//Array
static drv_gpio_t switch_conf_list[SWITCH_NUM] = //LED
{
    [LED_RUN] =     {.PIN = "A11",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = HIGHT_LEVEL},
    [LED_STATE] =   {.PIN = "A12",  .MODE = OUTPUT_DOWN, .INIT_LEVEL = HIGHT_LEVEL},
    [BUZZER] =      {.PIN = "D4",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},
    [LIMIT1] =      {.PIN = "E2",   .MODE = INPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},//限位开关1
    [LIMIT2] =      {.PIN = "E3",   .MODE = INPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},//限位开关2
    [HEAT1] =       {.PIN = "E9",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},//加热包1
    [HEAT2] =       {.PIN = "E11",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},//加热包1
    [HEAT3] =       {.PIN = "E14",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},//加热包1
    //[TEST] =        {.PIN = "C12",   .MODE = OUTPUT_DOWN, .INIT_LEVEL = LOW_LEVEL},
};
//Const
/* Private function prototypes -----------------------------------------------*/

/*****************************************************************************
* @brief SWITCH初始化
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
void SWITCH_init(void)
{
    unsigned int i = 0;
    
    for (; i < SWITCH_NUM; i++){
        Drv_Gpio_Init(&switch_conf_list[i]);
    }
	
	SWITCH_ON_OFF(BUZZER, SWITCH_ON);	//开机响一声作为自检
	SoftDelay_ms(200);
	SWITCH_ON_OFF(BUZZER, SWITCH_OFF);
}

/*****************************************************************************
* @brief 修改SWITCH状态(Toggle周期为task调用周期)
* ex:
* @par type：SWITCH类型 st:状态（SWITCH_ON，SWITCH_OFF，SWITCH_TOGGLE）
* None
* @retval 
*****************************************************************************/
void SWITCH_ON_OFF(SWITCH_TYPE type, SWITCH_ST st)
{
    if (type >= SWITCH_NUM || st >= SWITCH_ST_NUM)
        return;

    switch (st){
		case SWITCH_ON:
			switch_conf_list[type].Write(&switch_conf_list[type], 1);
			break;
		case SWITCH_OFF:
			switch_conf_list[type].Write(&switch_conf_list[type], 0);
			break;
		case SWITCH_TOGGLE:
			//GPIO_ToggleBits(DOUT_data[type].port, DOUT_data[type].pin);
			break;
		default:
			break;
    }
}
